<?php

namespace app\admin\controller;

use think\Config;
use think\facade\Db;

/**
 * 后台首页
 * @internal
 */
class CreateTestData
{
    /**
     * 创建土壤墒情统计表测试数据
     */
    public function soil()
    {
        die;
        //$testt = Db::name('spore_data')->order('statisticstime desc')->find();
        //var_dump($testt);die;
        $data = Db::name('spore_data')->where("statisticsDate >= '2023-08-01' and statisticsDate <= '2023-08-10'")->select()->toArray();
        //var_dump($data);die;
        foreach ($data AS $key => $value) {
            unset($value['id']);
            $dateTime = 1698775200+$key*3600;
            $addData = $value;
            $addData['newTime'] = date('Y-m-d H:i:s',$dateTime);
            $addData['createtime'] = date('Y-m-d H:i:s',$dateTime);
            $addData['statisticstime'] = date('Y-m-d H:i:s',$dateTime);
            $addData['statisticsDate'] = date('Y-m-d',$dateTime);
                Db::name('spore_data')->insert($addData);
        }
        var_dump('wancheng');die;
        die;
        //生成测试数据
        for ($i=0;$i<803;$i++) {
            $dateTime = 1696003200+$i*3600;
            $data = [
                'device_id'=>32,
                'device_name'=>'土壤墒情监控设备5',
                'soil_info_key'=>12344325,
                'soil_temperature_up'=> mt_rand(10,20),
                'soil_temperature_10'=> mt_rand(10,20),
                'soil_temperature_20' => mt_rand(10,22),
                'soil_temperature_30' => mt_rand(10,24),
                'soil_temperature_40' => mt_rand(10,26),
                'soil_water_10' => mt_rand(10,60),
                'soil_water_20' => mt_rand(10,60),
                'soil_water_30' => mt_rand(10,60),
                'soil_water_40' => mt_rand(10,60),
                'soil_conductivity_20' => mt_rand(0,10000),
                'soil_solt_20' => mt_rand(0,1500),
                'createtime'=> date('Y-m-d H:i:s',$dateTime),
                'createDate'=> date('Y-m-d',$dateTime),
                'hour' => date('Y-m-d H',$dateTime),
                'statisticstime'=> date('Y-m-d H:i:s',$dateTime),
            ];
            Db::name('soil_data')->insert($data);
        }
        exit('ok');
    }

    /**
     * 创建气象站统计表测试数据
     */
    public function weacher()
    {
        //        生成测试数据
        for ($i=0;$i<1500;$i++) {
            $dateTime = 1690819200+$i*3600;
            $data = [
                'temperature'=> mt_rand(10,45),
                'humidity'=> mt_rand(12,35),
                'sunlight' => mt_rand(10,140),
                'createtime'=> date('Y-m-d H:i:s',$dateTime),
                'createDate'=> date('Y-m-d',$dateTime),
                'hour' => date('Y-m-d H',$dateTime),
                'speed' =>  mt_rand(1,3),
                'direction' =>mt_rand(0,360),
                'rainfall' => mt_rand(1,2),
                'soil_temperature' => mt_rand(10,45),
                'soil_humidity' => mt_rand(12,35),
                'co2' => mt_rand(12,35),
                'hpa' => mt_rand(1000,1100),
                'radiation'=>mt_rand(1000,1100),
                'conductivity'=>mt_rand(1,2),
                'soil_solt'=>mt_rand(15,180),
                'ph'=>mt_rand(5,10),
                'evaporation'=>mt_rand(150,210),
                'statisticstime'=> date('Y-m-d H:i:s',$dateTime),
            ];
            Db::name('weather_device_statistics')->insert($data);
        }
        exit('ok');
    }


    /**
     * 创建虫情测试数据
     */
    public function bugData()
    {
        $value = Db::name('bug_data')->where('id',26)->find();
        unset($value['id']);
        for ($j=0; $j<=23; $j++) {
            $jTime = 1699871701+$j*86400;
            for ($i=0; $i<=8; $i++)
            {
                $sTime = $jTime+$i*5400;
                $value['createtime'] = date('Y-m-d H:i:s',$sTime);
                $value['statisticstime'] = date('Y-m-d H:i:s',$sTime);
                $value['newTime'] = date('Y-m-d H:i:s',$sTime);
                $value['statisticsDate'] = date('Y-m-d',$sTime);
                Db::name('bug_data')->insert($value);
            }
        }
        exit('ok');





        $url = "http://insect.sennor.net:1881/getAllInsectFileData?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF353B6D234D&page=4&size=100";
        $result = getCurl($url);

        foreach($result['datas'] AS $key => $value){
            $value['createtime'] = date('Y-m-d H:i:s',time());
            $value['statisticstime'] = $value['newTime'];
            $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
            Db::name('bug_data')->insert($value);
            if ($value['results'] == '未识别到对应虫类' || empty($value['results'])) {
                continue;
            }
            $bugArr = explode(',',$value['results']);
            foreach ($bugArr AS $kk => $vv) {
                $typeArr = explode(':',$vv);
                $typeInfo = Db::name('bug_type')->where('name',$typeArr[0])->find();
                preg_match('/\d+/',$typeArr[1],$number);
                if (empty($typeInfo)) {
                    $typeId = Db::name('bug_type')->insertGetId(['name'=>$typeArr[0]]);
                    Db::name('bug_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    $isStatistics = Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                    if (empty($isStatistics)) {
                        Db::name('bug_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                    } else {

                        Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                    }
                }
            }
        }
        exit('ok');
    }


    /**
     * 创建孢子测试数据
     */
    public function sporeData()
    {

        $value = Db::name('spore_data')->where('id',2918)->find();
        unset($value['id']);
        //35天
        for ($i=0; $i<=840; $i++)
        {
            $sTime = 1698886800+$i*3600;
            $value['createtime'] = date('Y-m-d H:i:s',$sTime);
            $value['statisticstime'] = date('Y-m-d H:i:s',$sTime);
            $value['newTime'] = date('Y-m-d H:i:s',$sTime);
            $value['statisticsDate'] = date('Y-m-d',$sTime);
            Db::name('spore_data')->insert($value);
        }

        exit('ok');




        die;
        $url = "http://insect.sennor.net:1881/getAllInsectFileData?token=2d2534b7daa64d67875a114a4944e140&userName=tjy123&deviceNumber=FF353B6D234D&page=4&size=100";
        $result = getCurl($url);
        foreach($result['datas'] AS $key => $value){
            $value['createtime'] = date('Y-m-d H:i:s',time());
            $value['statisticstime'] = $value['newTime'];
            $value['statisticsDate'] = date('Y-m-d',strtotime($value['newTime']));
            Db::name('bug_data')->insert($value);
            if ($value['results'] == '未识别到对应虫类' || empty($value['results'])) {
                continue;
            }
            $bugArr = explode(',',$value['results']);
            foreach ($bugArr AS $kk => $vv) {
                $typeArr = explode(':',$vv);
                $typeInfo = Db::name('bug_type')->where('name',$typeArr[0])->find();
                preg_match('/\d+/',$typeArr[1],$number);
                if (empty($typeInfo)) {
                    $typeId = Db::name('bug_type')->insertGetId(['name'=>$typeArr[0]]);
                    Db::name('bug_statistics')->insert(['type_id'=>$typeId,'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                } else {
                    $isStatistics = Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->find();
                    if (empty($isStatistics)) {
                        Db::name('bug_statistics')->insert(['type_id'=>$typeInfo['id'],'name'=>$typeArr[0],'num'=>$number[0],'statisticsdate'=>$value['statisticsDate'],'createtime'=>date('Y-m-d H:i:s',time())]);
                    } else {

                        Db::name('bug_statistics')->where(['type_id'=>$typeInfo['id'],'statisticsdate'=>$value['statisticsDate']])->inc('num',$number[0])->update();
                    }
                }
            }
        }
        exit('ok');
    }


    public function deletedata()
    {
        $updateData = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 12:00:21',
            'hour'=>'2023-12-07 12',
            'statisticstime'=>'2023-12-07 12:00:21',
            ];
        Db::name('weather_device_data')->where('id',2422)->update($updateData);

        $updateData1 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 11:30:21',
            'hour'=>'2023-12-07 11',
            'statisticstime'=>'2023-12-07 11:30:21',
        ];
        Db::name('weather_device_data')->where('id',2421)->update($updateData1);

        $updateData2 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 11:00:21',
            'hour'=>'2023-12-07 11',
            'statisticstime'=>'2023-12-07 11:00:21',
        ];
        Db::name('weather_device_data')->where('id',2420)->update($updateData2);

        $updateData3 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 10:30:21',
            'hour'=>'2023-12-07 10',
            'statisticstime'=>'2023-12-07 10:30:21',
        ];
        Db::name('weather_device_data')->where('id',2419)->update($updateData3);

        $updateData4 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 10:00:21',
            'hour'=>'2023-12-07 10',
            'statisticstime'=>'2023-12-07 10:00:21',
        ];
        Db::name('weather_device_data')->where('id',2418)->update($updateData4);

        $updateData5 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 09:30:21',
            'hour'=>'2023-12-07 09',
            'statisticstime'=>'2023-12-07 09:30:21',
        ];
        Db::name('weather_device_data')->where('id',2417)->update($updateData5);

        $updateData6 = [
            'createDate'=>'2023-12-07',
            'createtime'=>'2023-12-07 09:00:21',
            'hour'=>'2023-12-07 09',
            'statisticstime'=>'2023-12-07 09:00:21',
        ];
        Db::name('weather_device_data')->where('id',2416)->update($updateData6);
    }


    public function

}
